package com.demo.web.wx;

import com.demo.dto.BaseResult;
import com.demo.dto.ListResult;
import com.demo.dto.Pagination;
import com.demo.entity.Clientele;
import com.demo.entity.SalePerson;
import com.demo.service.IClienteleService;
import com.demo.vo.ClienteleVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/wx/clientele")
public class ClienteleWxController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    IClienteleService dataService;

    /**
     * 创建
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public BaseResult<Clientele> get(Clientele arg) {
        Clientele service_res = dataService.selectByPrimaryKey(arg.getId());
        BaseResult<Clientele> res = new BaseResult<Clientele>(true, service_res);
        return res;
    }

    /**
     * create
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public BaseResult<Integer> insert(Clientele arg,HttpSession session) {
        SalePerson sale = (SalePerson)session.getAttribute("current");
        arg.setSalesId(String.valueOf(sale.getId()));
        int service_res = dataService.insertSelective(arg);
        BaseResult<Integer> res = new BaseResult<Integer>(true, service_res);
        return res;
    }

    /**
     * update
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResult<Integer> update(Clientele arg) {
        int service_res = dataService.updateByPrimaryKeySelective(arg);
        BaseResult<Integer> res = new BaseResult<Integer>(true, service_res);
        return res;
    }
    
    /***
     * 分页查询
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ListResult<Clientele> query(ClienteleVo arg) {
        List<Clientele> list = dataService.queryByPage(arg);
        int total = dataService.queryCount(arg);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        ListResult<Clientele> res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }


    /**
     *
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryMyClientele", method = RequestMethod.GET)
    public ListResult<Object> queryMyClientele(ClienteleVo arg,HttpSession session) {
        SalePerson sale = (SalePerson)session.getAttribute("current");
        ListResult<Object> res = null;
        if(sale!=null){
            arg.setSalesId(String.valueOf(sale.getId()));
            List<Clientele> list = dataService.queryMyClientele(arg);
            res = new ListResult(true, list);
        }else{
            res = new ListResult(false, "请登录!",401);
        }
        return res;
    }
}
